#coding=utf-8
import os
import shutil
import sys
import time
import math
import json
import zipfile
from subprocess import run
import subprocess
# import thread
# import thread6
sys.path.append('../..')
import FileUtils
import ArrayUtils
from PIL import Image
import uuid

curPath = os.getcwd()
print(curPath)

projectPath = os.path.abspath(os.path.join(os.path.dirname(curPath), os.pardir,os.pardir))
print(projectPath)

import platform
sysstr = platform.system()
print(platform)



_DIR = os.path.join(curPath,"../../../assets/res/BattleScene/BattleSpineDir_all")
_TMP_DIR = os.path.join(projectPath,"assets/res")


def clearTmpDir():
	FileUtils.rmtree(_TMP_DIR)
	pass
	
def copyOriginResToTmpDir():
	FileUtils.makedirs(_TMP_DIR)
	FileUtils.copyDirOneByOne(_DIR,_TMP_DIR)



def changePng2SimplePng():
	def callback(filepath,filename):
		print("开始对图片进行处理:"+filepath)
		im = Image.open(filepath)
		width = im.size[0] #获取宽度
		height = im.size[1] #获取长度
		
		for x in range(width):
			for y in range(height):
				# r,g,b,a = im.getpixel((x,y))	
				# rgba=(r,g,b,a)
				# if(a == 0):
				# 	im.putpixel((x,y),(0,0,0,0))
				# if(a > 0):
				# 	im.putpixel((x,y),(255,255,255,255))

				# color = [im.getpixel((x,y))]
				# if len(color) <= 3:
				# 	continue	
				# if(color[3] == 0):
				# 	im.putpixel((x,y),(0,0,0,0))
				# if(color[3] > 0):
				# 	im.putpixel((x,y),(255,255,255,255))
				
				color = im.getpixel((x,y))
				# print(color)
				# print(color[0])
				if(type(color) == int):
					continue
				if(color[3] == 0):
					im.putpixel((x,y),(0,0,0,0))
				if(color[3] > 0):
					im.putpixel((x,y),(255,255,255,255))
		im = im.convert('RGBA')
		im.save(filepath)
		pass

	FileUtils.diguiDirWithTail(_TMP_DIR,callback,".png")


def changeSpineFileMetaUuid():
	
	def callback(filepath,filename):
		print("开始处理spine的uuid问题:"+filepath+" ## ")
		data = FileUtils.getJsonFileData(filepath)
		uid = uuid.uuid4()
		print(uid)
		# print(uid["UUID"])
		print(str(uid))
		data["uuid"] = str(uid)
		print(data)
		FileUtils.writeJsonToFile(filepath,data)
		pass

	FileUtils.diguiDirWithTail(_TMP_DIR,callback,".json.meta")


def renameTmpDirName():
	filelist = os.listdir(_TMP_DIR)
	for filename in filelist:
		filepath = os.path.join(_TMP_DIR, filename)
		if os.path.isdir(filepath):
			FileUtils.reName(filepath,filepath+"_tmp")
			
	pass

#有未解决,还要解决他们的uuid的关系才行
if __name__ == "__main__":
	# clearTmpDir()
	# copyOriginResToTmpDir()
	changePng2SimplePng()
	# changeSpineFileMetaUuid()
	# renameTmpDirName()
	# renameBundleName()
	pass



